/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var selectIds = "";
//表格初始化配置
var tableInit = function () {
    var columns = [
        {
            title: '全选',
            field: 'select',
            //复选框
            checkbox: true,
            width: 25,
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'name',
            title: '商品名字',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'showUrl',
            title: '商品缩略图',
            align: 'center',
            formatter: function (value, row, index) {
                if (!empty(value)) {
                    var img_html = "<a href='" + value + "' title='点击查看大图' target='_blank'>" +
                        "<img src='" + value + "' width='40' onerror='imgNotfind(event);' /></a>";
                    return img_html;
                } else {
                    return "<img src='" + value + "' width='40' onerror='imgNotfind(event);' />";
                }
            }
        },
        {
            field: 'brandName',
            title: '商品品牌',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'typeName',
            title: '商品类别',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'oldPrice',
            title: '商品原价',
            align: 'center',
            valign: 'middle',
            visible: false
        },
        {
            field: 'price',
            title: '商品售价',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'inventory',
            title: '商品库存',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'introduction',
            title: '商品简介',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'state',
            title: '上架/下架',
            align: 'center',
            formatter: function (value, row, index) {
                if (value == 0) return '<span class="label label-default">下架</span>';
                if (value == 1) return '<span class="label label-success">上架</span>';
            }
        },
        {
            field: 'isNew',
            title: '是否新品',
            align: 'center',
            formatter: function (value, row, index) {
                if (value == 0) return '<span class="label label-default">否</span>';
                if (value == 1) return '<span class="label label-success">是</span>';
            }
        },
        {
            field: 'isHot',
            title: '是否热销',
            align: 'center',
            formatter: function (value, row, index) {
                if (value == 0) return '<span class="label label-default">否</span>';
                if (value == 1) return '<span class="label label-success">是</span>';
            }
        },
        {
            field: 'isTop',
            title: '是否置顶',
            align: 'center',
            formatter: function (value, row, index) {
                if (value == 0) return '<span class="label label-default">否</span>';
                if (value == 1) return '<span class="label label-success">是</span>';
            }
        },
        {
            field: 'schoolId',
            title: '分部',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                if (value == null) {
                    return '<span class="label label-warning">公用</span>';
                }
                if (value == schoolId) {
                    return '<span class="label label-primary">' + schoolMap[value] + '</span>';
                } else {
                    return '<span class="label label-warning">' + schoolMap[value] + '</span>';
                }
            }
        },
        {
            field: 'gmtCreate',
            title: '创建时间',
            align: 'center',
            formatter: function (value, row, index) {
                return formatDateTime(value);
            }
        },
        {
            field: 'gmtModified',
            title: '处理时间',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                return formatDateTime(value);
            }
        }
    ];

    //请求服务数据时所传参数
    function queryParams(params) {
        return {
            pageNo: params.pageNumber,
            pageSize: params.pageSize,
            companyId: companyId,
            schoolId: schoolId,
            type: 1,
            gstate: $("#gstate").val(),
            searchText: params.searchText,
            sortName: params.sortName,      //排序列名
            sortOrder: params.sortOrder //排位命令（desc，asc）
        }
    }

    var tableConfig = $.extend(bootstrapTableConfig(_HOST + _API.product_goods_list, queryParams, columns), {
        //这里是其他的一些个性化的 bootstrap-table options
        //例如 覆盖一些公共的配置
        //例如 添加一些私有的配置
        showColumns: true,//列显示操作按钮
        ajaxOptions: { //设置自定义请求头
            headers: {
                'Authorization': sessionStorageUtils.getJson(storageKey.sessionStorageKey.token),
                'permCoding': 'goods:list'
            }
        },
        //例如格式化数据
        responseHandler: function (res) {
            //在ajax获取到数据，渲染表格之前，修改数据源
            //console.log(res)
            if (res.success) {
                //更新本地token
                sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.token, res.token);
            }
            return res;
        }

    });
    //调用实例化
    $('#bootstrapTableEvents').bootstrapTable(tableConfig);
    $(".search input").attr('placeholder', '输入名称、品牌、类别搜索');
};

var app = new Vue({
    el: '#app',
    data: {
        permissions: ['goods:add', 'goods:update', 'goods:delete', 'goods:list'],//本页权限数组
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if (empty(token)) {
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon: 5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            }, 1200);
            return;
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        selectStatus: function () {
            $('#bootstrapTableEvents').bootstrapTable('refresh');
        },
        //添加
        add: function (index) {
            var that = this;
            var $result = $('#bootstrapTableEvents');
            var list = $result.bootstrapTable('getSelections');
            if (list.length <= 0) {
                layer.msg('请至少选中一条数据', {time: 1500, icon: 5});
                return;
            }

            var arr = [];
            for (let i = 0; i < list.length; i++) {
                var obj = {};
                obj.id = list[i].id;
                obj.name = list[i].name;
                obj.showUrl = list[i].showUrl;
                obj.introduction = list[i].introduction;
                arr.push(list[i]);
            }
            console.log(arr);
            sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.goods.goodsList, arr);
            setTimeout(function () {
                parent.layer.closeAll();
            }, 500)
        },

    },

});


$(function () {
    //初始化表格
    tableInit();
});

